package org.noobie.photopicker.controller;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;

import org.noobie.photopicker.entity.Album;
import org.noobie.photopicker.entity.Photo;

import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.ImageColumns;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlbumController {private ContentResolver resolver;

    public AlbumController(Context context) {
        resolver = context.getContentResolver();
    }

    public List<Photo> getCurrent() {
        Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.DATA,
                ImageColumns.DATE_ADDED, ImageColumns.SIZE }, null, null, ImageColumns.DATE_ADDED);
        if (cursor == null || !cursor.moveToNext())
            return new ArrayList<>();
        List<Photo> photos = new ArrayList<>();
        cursor.moveToLast();
        do {
            if (cursor.getLong(cursor.getColumnIndex(ImageColumns.SIZE)) > 1024 * 10) {
                photos.add(new Photo(cursor.getString(cursor.getColumnIndex(ImageColumns.DATA))));
            }
        } while (cursor.moveToPrevious());
        cursor.close();
        return photos;
    }

    public List<Album> getAlbums() {
        List<Album> albums = new ArrayList<>();
        Map<String, Album> map = new HashMap<>();
        Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.DATA,
                ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.SIZE }, null, null, null);
        if (cursor == null || !cursor.moveToNext())
            return new ArrayList<>();
        cursor.moveToLast();
        Album current = new Album("最近照片", cursor.getString(cursor.getColumnIndex(ImageColumns.DATA)), 0, true);
        albums.add(current);
        do {
            if (cursor.getInt(cursor.getColumnIndex(ImageColumns.SIZE)) < 1024 * 10)
                continue;

            current.increaseCount();
            String name = cursor.getString(cursor.getColumnIndex(ImageColumns.BUCKET_DISPLAY_NAME));
            if (map.keySet().contains(name))
                map.get(name).increaseCount();
            else {
                Album album = new Album(name, cursor.getString(cursor.getColumnIndex(ImageColumns.DATA)), 1);
                map.put(name, album);
                albums.add(album);
            }
        } while (cursor.moveToPrevious());
        cursor.close();
        return albums;
    }

    public List<Photo> getAlbum(String name) {
        Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.BUCKET_DISPLAY_NAME,
                        ImageColumns.DATA, ImageColumns.DATE_ADDED, ImageColumns.SIZE }, "bucket_display_name = ?",
                new String[] { name }, ImageColumns.DATE_ADDED);
        if (cursor == null || !cursor.moveToNext())
            return new ArrayList<>();
        List<Photo> photos = new ArrayList<>();
        cursor.moveToLast();
        do {
            if (cursor.getLong(cursor.getColumnIndex(ImageColumns.SIZE)) > 1024 * 10) {
                photos.add(new Photo(cursor.getString(cursor.getColumnIndex(ImageColumns.DATA))));
            }
        } while (cursor.moveToPrevious());
        cursor.close();
        return photos;
    }
    
}
